package cn.com.guage.guc.juc;

import java.util.concurrent.CyclicBarrier;

//10人都到达餐厅，才可以开始开餐
public class CyclicBarrierDemo {

    //创建固定值10  比如10个人去餐厅吃饭
    private static final int NUMBER = 10;

    public static void main(String[] args) {
        //创建CyclicBarrier
        CyclicBarrier cyclicBarrier =new CyclicBarrier(NUMBER,()->{System.out.println("*****10人到齐,开始就餐");});

        //10人到达餐厅过程
        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName()+"到达餐厅,等待开餐");
                    //等待
					cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
